package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.device.Global;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.dao.CollectDeviceDao;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.dao.mapper.custom.CygMapper;
import com.hzhr.iot.device.entity.AlarmDataEntity;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.model.alarmData.AlarmDataGetModel;
import com.hzhr.iot.device.model.alarmData.AlarmDataPostModel;
import com.hzhr.iot.device.model.nbMeter.*;
import com.hzhr.iot.device.queryModel.custom.CollectDeviceCustomQueryModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.device.service.helper.User_DeptServiceHelper;
import com.hzhr.iot.device.util.RedisUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NbMeterService extends BaseService {
    @Autowired
    IotDeviceDao iotDeviceDao;
    @Autowired
    CygMapper cygMapper;
    @Autowired
    User_DeptServiceHelper user_deptServiceHelper;
    @Autowired
    CollectDeviceDao collectDeviceDao;

    //region javadoc注释 : post
    /**
     * 新增NB表 <br/><br/>
     * 1. 验证NB物联网表的IMEI号：如果存在相同IMEI号的物联网设备，抛出异常“IMEI号已存在” <br/>
     * 2. 新增NB物联网表 <br/>
     * 3. 通知物联网平台创建该设备档案 <br/>
     * 4. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * NBMETERSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证NB物联网表的IMEI号<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : IMEI号已存在<br/>
     * @see NbMeterPostModel body <br/>
     * @see NbMeterGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //新增NB表
    //1. 验证NB物联网表的IMEI号：如果存在相同IMEI号的物联网设备，抛出异常“IMEI号已存在”
    //2. 新增NB物联网表
    //3. 通知物联网平台创建该设备档案
    //4. 返回信息
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/nb-meters", method = RequestMethod.POST)
    //endregion
    @Transactional(rollbackFor = Exception.class)
    public NbMeterGetModel post(Map<String, String> header, Map<String, Object> query,
                                NbMeterPostModel body) throws ServiceException {
        //1. 验证NB物联网表的IMEI号：如果存在相同IMEI号的物联网设备，抛出异常“IMEI号已存在”
        String imei = body.getImei();
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_iotPlatformImei(imei);
        queryModel.setEqual_isValid("1");
        int count = iotDeviceDao.selectCount(queryModel);
        if (count > 0) {
            throw new ServiceException(Device_ErrorsEnum.NBMETERSERVICE_POST_201);
        }

        //2. 新增NB物联网表
        UserCardModel userCard = getUserCard(header);

        IotDeviceEntity iotDeviceEntity = new IotDeviceEntity();
        iotDeviceEntity.setDes(body.getDes());
        iotDeviceEntity.setIotPlatformImei(body.getImei());
        iotDeviceEntity.setRemark(body.getRemark());

        iotDeviceEntity.setCategoryCode("1");
        iotDeviceEntity.setCompanyId(userCard.getCompanyId());
        iotDeviceEntity.setIotPlatformCode("1");
        iotDeviceEntity.setSimTypeCode("1");
        iotDeviceEntity.setCreateTime(new Date());
        iotDeviceEntity.setCreateUserId(userCard.getUserId());
        iotDeviceEntity.setIsValid("1");
        iotDeviceEntity.setState("1");
        String resourceCode = user_deptServiceHelper.getDeptResourceCode(initHeader(header, AccessModeEnum.GLOBAL), userCard.getCompanyId(), userCard.getDeptId());
        iotDeviceEntity.setResourceCode(resourceCode);
        int changeCount = iotDeviceDao.insert(iotDeviceEntity);

        //3. 通知物联网平台创建该设备档案
        imei = body.getImei();
        String des = body.getDes();
        count = cygMapper.select2(imei);
        cygMapper.insert1(imei, des, 1, 1);

        //4. 返回信息
        NbMeterGetModel result = new NbMeterGetModel();
        result.setChangeCount(changeCount);
        return result;
    }

    //region javadoc注释 : put
    /**
     * 修改NB表 <br/><br/>
     * 1. 获取NB物联网表：查询NB物联网表，如果不存在，抛出异常“NB物联网表不存在” <br/>
     * 2. 验证NB物联网表是否在iot平台上完成注册：如果该NB物联网表没有iot平台设备编号，抛出异常“该NB物联网表还未在iot平台上完成注册” <br/>
     * 3. 修改NB物联网表 <br/>
     * 4. 通知iot平台更新该设备档案 <br/>
     * 5. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * NBMETERSERVICE_PUT_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 获取NB物联网表<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB物联网表不存在<br/>
     * NBMETERSERVICE_PUT_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证NB物联网表是否在iot平台上完成注册<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 该NB物联网表还未在iot平台上完成注册<br/>
     * @see NbMeterPutModel body <br/>
     * @see NbMeterGetModel result <br/>
     */ 
    //endregion
    //region 说明 : put
    //修改NB表
    //1. 获取NB物联网表：查询NB物联网表，如果不存在，抛出异常“NB物联网表不存在”
    //2. 验证NB物联网表是否在iot平台上完成注册：如果该NB物联网表没有iot平台设备编号，抛出异常“该NB物联网表还未在iot平台上完成注册”
    //3. 修改NB物联网表
    //4. 通知iot平台更新该设备档案
    //5. 返回信息
    //endregion
    //region 参数 : put
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : put
    @RequestMapping(value = "/nb-meters/{nbMeterId}", method = RequestMethod.PUT)
    //endregion
    @Transactional(rollbackFor = Exception.class)
    public NbMeterGetModel put(Map<String, String> header, Integer nbMeterId, Map<String, Object> query,
                               NbMeterPutModel body) throws ServiceException {
        //1. 获取NB物联网表：查询NB物联网表，如果不存在，抛出异常“NB物联网表不存在”
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_id(nbMeterId);
        queryModel.setEqual_isValid("1");
        queryModel.setShow(true);
        IotDeviceEntity nbMeter = iotDeviceDao.selectEntity(queryModel);
        if (nbMeter == null) {
            throw new ServiceException(Device_ErrorsEnum.NBMETERSERVICE_PUT_201);
        }

        //2. 验证NB物联网表是否在iot平台上完成注册：如果该NB物联网表没有iot平台设备编号，抛出异常“该NB物联网表还未在iot平台上完成注册”
        String iotPlatformDeviceId = nbMeter.getIotPlatformDeviceId();
        if (StringUtils.isEmpty(iotPlatformDeviceId)) {
            throw new ServiceException(Device_ErrorsEnum.NBMETERSERVICE_PUT_202);
        }

        //3. 修改NB物联网表
        UserCardModel userCard = getUserCard(header);
        nbMeter.setDes(body.getDes());
        nbMeter.setRemark(body.getRemark());
        nbMeter.setUpdateTime(new Date());
        nbMeter.setUpdateUserId(userCard.getUserId());
        int changeCount = iotDeviceDao.update(nbMeter);

        //4. 通知iot平台更新该设备档案
        String imei = nbMeter.getIotPlatformImei();
        String des = body.getDes();
        cygMapper.insert2(imei, des, iotPlatformDeviceId,1, 3);

        //5. 返回信息
        NbMeterGetModel result = new NbMeterGetModel();
        result.setChangeCount(changeCount);
        return result;
    }

    //region javadoc注释 : delete
    /**
     * 删除NB表 <br/><br/>
     * 1. 判断NB表是否具备删除条件： <br/>
     *  如果该NB表拥有下级采集设备，无法删除； <br/>
     * 2. 删除NB物联网表 <br/>
     * 3. 如果该NB物联网表在iot平台上完成了注册，通知iot平台删除该设备档案 <br/>
     * 4. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @throws ServiceException <br/>
     * NBMETERSERVICE_DELETE_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 判断NB表是否具备删除条件<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 该NB物联网表不具备删除条件<br/>
     * @see NbMeterDeleteModel result <br/>
     */ 
    //endregion
    //region 说明 : delete
    //删除NB表
    //1. 判断NB表是否具备删除条件：
    // 如果该NB表拥有下级采集设备，无法删除；
    //2. 删除NB物联网表
    //3. 如果该NB物联网表在iot平台上完成了注册，通知iot平台删除该设备档案
    //4. 返回信息
    //endregion
    //region 参数 : delete
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : delete
    @RequestMapping(value = "/nb-meters/{nbMeterId}", method = RequestMethod.DELETE)
    //endregion
    @Transactional(rollbackFor = Exception.class)
    public NbMeterDeleteModel delete(Map<String, String> header, Integer nbMeterId, Map<String, Object> query) throws ServiceException {
        /* 判断NB表是否具备删除条件：
            如果该NB表拥有下级采集设备，无法删除； */
        CollectDeviceCustomQueryModel collectDeviceCustomQueryModel = new CollectDeviceCustomQueryModel();
        collectDeviceCustomQueryModel.setEqual_isValid("1");
        collectDeviceCustomQueryModel.setEqual_iotDeviceId(nbMeterId);
        int count = collectDeviceDao.selectCount(collectDeviceCustomQueryModel);
        if (count > 0) {
            throw new ServiceException(Device_ErrorsEnum.NBMETERSERVICE_DELETE_201);
        }

        //查询原有nb表
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_id(nbMeterId);
        queryModel.setEqual_isValid("1");
        queryModel.setShow(true);
        IotDeviceEntity oriNbMeter = iotDeviceDao.selectEntity(queryModel);

        /* 删除NB物联网表 */
        queryModel = new IotDeviceCustomQueryModel();
        queryModel.setSet_isValid("0");
        queryModel.setEqual_id(nbMeterId);
        int changeCount = iotDeviceDao.updateProps(queryModel);

        /* 如果该NB物联网表在iot平台上完成了注册，通知iot平台删除该设备档案 */
        if (oriNbMeter != null && !StringUtils.isEmpty(oriNbMeter.getIotPlatformDeviceId())) {
            String imei = oriNbMeter.getIotPlatformImei();
            String iotPlatformDeviceId = oriNbMeter.getIotPlatformDeviceId();
            cygMapper.insert3(imei, iotPlatformDeviceId,1, 2);
        }

        /* 返回信息 */
        NbMeterDeleteModel result = new NbMeterDeleteModel();
        result.setChangeCount(changeCount);
        return result;
    }
}

